package com.chenailin.www.controller.search;

import com.chenailin.www.model.vo.ResultVO;
import com.chenailin.www.model.vo.SearchResultVO;
import com.chenailin.www.service.SearchService;
import com.chenailin.www.serviceimpl.SearchServiceImpl;
import com.chenailin.www.util.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author evi
 */
@WebServlet("/search/*")
public class SearchController extends HttpServlet {
    private final SearchService searchService = new SearchServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Set response content type
        resp.setContentType("application/json;charset=UTF-8");
        String path = req.getRequestURI().substring(req.getContextPath().length());
        Long userId = (Long) req.getSession().getAttribute("userId");
        String keyword = req.getParameter("keyword");

        // Check if keyword is provided
        if (keyword == null || keyword.trim().isEmpty()) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().write(JsonUtil.toJson(ResultVO.error(400, "Keyword is required")));
            return;
        }

        try {
            if (path.equals("/search/global")) {
                // Global search (search in all categories)
                int page = req.getParameter("page") != null ? Integer.parseInt(req.getParameter("page")) : 1;
                int size = req.getParameter("size") != null ? Integer.parseInt(req.getParameter("size")) : 10;

//                SearchResultVO result = searchService.globalSearch(keyword, userId, page, size);
//                resp.getWriter().write(JsonUtil.toJson(ResultVO.success(result)));
                System.out.println("此功能尚未实现");
            } else if (path.equals("/search/knowledge")) {
                // Search only in knowledge bases
                int page = req.getParameter("page") != null ? Integer.parseInt(req.getParameter("page")) : 1;
                int size = req.getParameter("size") != null ? Integer.parseInt(req.getParameter("size")) : 10;

                // !!!
                SearchResultVO result = (SearchResultVO) searchService.searchKnowledgeBases(keyword, page, size);
                resp.getWriter().write(JsonUtil.toJson(ResultVO.success(result)));
            } else if (path.equals("/search/article")) {
                // Search only in articles
                int page = req.getParameter("page") != null ? Integer.parseInt(req.getParameter("page")) : 1;
                int size = req.getParameter("size") != null ? Integer.parseInt(req.getParameter("size")) : 10;

                // !!!
                SearchResultVO result = (SearchResultVO) searchService.searchArticles(keyword, page, size);
                resp.getWriter().write(JsonUtil.toJson(ResultVO.success(result)));
            } else if (path.equals("/search/user")) {
                // Search only for users
                int page = req.getParameter("page") != null ? Integer.parseInt(req.getParameter("page")) : 1;
                int size = req.getParameter("size") != null ? Integer.parseInt(req.getParameter("size")) : 10;

                // ？？？
                SearchResultVO result = (SearchResultVO) searchService.searchUsers(keyword, page, size);
                resp.getWriter().write(JsonUtil.toJson(ResultVO.success(result)));
            } else if (path.equals("/search/enterprise")) {
                // Search only in enterprises
                int page = req.getParameter("page") != null ? Integer.parseInt(req.getParameter("page")) : 1;
                int size = req.getParameter("size") != null ? Integer.parseInt(req.getParameter("size")) : 10;

//                SearchResultVO result = searchService.searchEnterprises(keyword, userId, page, size);
//                resp.getWriter().write(JsonUtil.toJson(ResultVO.success(result)));
                System.out.println("此功能尚未实现");
            } else {
                // Invalid path
                resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                resp.getWriter().write(JsonUtil.toJson(ResultVO.error(400, "Invalid search type")));
            }
        } catch (NumberFormatException e) {
            resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            resp.getWriter().write(JsonUtil.toJson(ResultVO.error(400, "Invalid page or size parameter")));
        } catch (Exception e) {
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            resp.getWriter().write(JsonUtil.toJson(ResultVO.error(500, e.getMessage())));
        }
    }
}